package com.sx.service;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;

import com.dfec.model.Msg;
import com.dfec.utils.DruidPlugin;
import com.dfec.utils.StringUtils;


/**
 * Servlet implementation class WarningServlet
 * @param <T>
 */
public class FarmActivityService {
	QueryRunner qr=new QueryRunner(DruidPlugin.getDataSource());
	List<Object> list=null;
    private String sql ="";


	public List<Object> getOne() throws SQLException {
		// TODO Auto-generated method stub
		sql="select * from (select a.*,mon-(select month(now())) as nearmon,(day-(select day(now()))) as nearday from b_farm_activities a  ORDER BY nearmon asc) as t where t.nearmon>=0  and nearday >=0 limit 1";
		list=new ArrayList<>();
    	list.add(qr.query(sql, new  MapListHandler()));
    	return list;
	}

	public List<Object> getDiteals(String id) throws SQLException {
		sql="select * from b_farm_activities where id="+id;
		list=new ArrayList<>();
    	list.add(qr.query(sql, new  MapListHandler()));
    	return list;
	}

	public List<Object> list(String page, String size, String keyWord) throws SQLException {
		sql="select * from b_farm_activities where 1=1 ";
		System.out.println("keyWord="+keyWord);
		if(StringUtils.isNotEmpty(keyWord))
			sql+=" and name like'%"+keyWord+"%'";
		 int page_num = 1;
		 int size_num = 10;
		if(StringUtils.isNotEmpty(page)) {
			page_num=Integer.parseInt(page);
			size_num = Integer.parseInt(size);
			
		}
		int num=((page_num-1)*size_num);
    	
		sql+=" ORDER BY mon asc, day asc limit "+num+","+size_num;
		System.out.println(sql);
		list=new ArrayList<>();
    	list.add(qr.query(sql, new  MapListHandler()));
    	return list;
	}
}